﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PBOffice.BLL;
using PBOffice.DAL;

public partial class FilesPage_FileIndex : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode node = new TreeNode("文档管理","17");
            TreeViewFiles.Nodes.Add(node);
            ShowtreeViwe(node, 17);
            TreeviewSelectNode(TreeViewFiles);
        }
    }

    #region 树图展开对应的节点
    /// <summary>
    /// 根据条件展开树图对应的节点
    /// </summary>
/// <param name="tv">需要判断的TreeView</param>
    private void TreeviewSelectNode( TreeView tv)
    {
        foreach (TreeNode item in tv.Nodes)
        {
            if (Request.Cookies["tree_file"] != null && item.Value.ToString() == Request.Cookies["tree_file"].Value.ToString())
            {
                item.Selected = true;
                item.Expand();
            }
            else if (item.ChildNodes.Count>0)
            {
                foreach (TreeNode  son in item.ChildNodes)
                {
                    TreeviewSelectNode(son);
                }
            }
        }
    }
    /// <summary>
    ///  根据条件展开树图对应的节点
    /// </summary>
    /// <param name="node">需要展开的节点的ID</param>
    private void TreeviewSelectNode(TreeNode node)
    {
        if (Request.Cookies["tree_file"] != null && node.Value.ToString() == Request.Cookies["tree_file"].Value.ToString())
        {
            node.Selected = true;
            node.Expand();
        }
        else if (node.ChildNodes.Count > 0)
        {
            foreach (TreeNode son in node.ChildNodes)
            {
                TreeviewSelectNode(son);
            }
        }
    }
    #endregion

    #region 显示树图
    /// <summary>
    /// 显示树图
    /// </summary>
    /// <param name="fatherNode">需要展开的节点</param>
    private void ShowtreeViwe(TreeNode fanode, int fatherNode)
    {
        List<v_SelectAllDocumentManage> doc = FileOperationBLL.SelectAllDoc(fatherNode);
        foreach (var item in doc)
        {
            if (item.DocTypeNo == 1 && item.IsDelete==false)//判断是否为文件夹和是否删除
            {
                TreeNode node = new TreeNode();
                node.Text = item.DocId.ToString();
                node.Value = item.DocNo.ToString();
                fanode.ChildNodes.Add(node);
            }
        }
    } 
    #endregion

    /// <summary>
    /// 指示说明当前引用母版页的子页面
    /// </summary>
    public static string SonPage;

    #region 子页面加载判断
    /// <summary>
    /// 判断加载的子页面。给子页面的控件进行赋值等相关操作.
    /// </summary>
    /// <param name="sonPage"></param>
    private void SonPageDate(string sonPage)
    {
        string allPath = FileOperationBLL.SelectDocPathByDocNo(Convert.ToInt32(TreeViewFiles.SelectedNode.Value));//获取文件的路径
        switch (sonPage)
        {
            case "FileManage"://文件信息页面
                TextBox Position = this.ContentPlaceHolderSon.FindControl("TextBoxPosition") as TextBox;
                Position.Text = allPath;
                GridView gridFileManage = this.ContentPlaceHolderSon.FindControl("GridViewFiles") as GridView;
                gridFileManage.DataSource = FileOperationBLL.SelectAllFile(Convert.ToInt32(TreeViewFiles.SelectedNode.Value));
                gridFileManage.DataBind();
                break;
            case "ShowFile":
                TextBox Address = this.ContentPlaceHolderSon.FindControl("TextBoxShowFilesAddress") as TextBox;
                Address.Text = allPath;
                GridView gridShowfile = this.ContentPlaceHolderSon.FindControl("GridViewShowFiles") as GridView;
                gridShowfile.DataSource = FileOperationBLL.SelectAllDoc(Convert.ToInt32(TreeViewFiles.SelectedNode.Value));
                gridShowfile.DataBind();
                break;
            case "AddFolder":
                Label AddFolderaddress = this.ContentPlaceHolderSon.FindControl("LabelAddress") as Label;
                AddFolderaddress.Text = allPath;
                Label docId = this.ContentPlaceHolderSon.FindControl("LabelDocId") as Label;
                docId.Text = TreeViewFiles.SelectedNode.Value;
                break;
        }
        SonPage = "";
    } 
    #endregion

    protected void TreeViewFiles_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeViewFiles.SelectedNode.ChildNodes.Clear();
        ShowtreeViwe(TreeViewFiles.SelectedNode, Convert.ToInt32(TreeViewFiles.SelectedNode.Value));
        TreeViewFiles.SelectedNode.Expand();
        SonPageDate(SonPage);
        Response.Cookies["tree_file"].Value = TreeViewFiles.SelectedNode.Value;//保存选中的节点的信息
    }
    protected void TreeViewFiles_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        foreach (TreeNode item in e.Node.ChildNodes)//对树图进行2级加载
        {
            item.ChildNodes.Clear();
            ShowtreeViwe(item, Convert.ToInt32(item.Value));
        }
    }
}
